package com.durian.base.net;

import android.text.TextUtils;
import android.util.Log;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;


/**
 * 说明：Utils
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019/1/31 13:00
 * <p/>
 * 版本：version 1.0
 */
public class Utils {

    public static String getFullUrl(String url, List<Part> params, boolean urlEncoder){
        StringBuffer urlFull = new StringBuffer();
        urlFull.append(url);
        if (urlFull.indexOf("?",0) < 0 && !params.isEmpty()){
            urlFull.append("?");
        }
        int flag = 0;
        for (Part part:params){
            String key = part.getKey();
            String value = part.getValue();
            if (urlEncoder){
                try {
                    key = URLEncoder.encode(key,"UTF-8");
                    value = URLEncoder.encode(value,"UTF-8");
                }catch (UnsupportedEncodingException e){
                    eLog(e);
                }
            }
            urlFull.append(key).append("=").append(value);
            if (++flag != params.size()){
                urlFull.append("&");
            }
        }
        return urlFull.toString();
    }

    /**
     * 说明：判断file是否是type类型
     * @param file
     * @param type
     * @return
     */
    public static boolean isFileType(File file, String type){
        String name = file.getName();
        int index = name.lastIndexOf(".");
        return index > 0 && isEqualsIgnoreCase(type,name.substring(index + 1));
    }

    /**
     * 说明：比较字符串是否相等（忽视大小写）
     * @param a
     * @param b
     * @return
     */
    public static boolean isEqualsIgnoreCase(String a, String b){
        if (a == b) return true;
        if (a != null && b != null) {
            return a.equalsIgnoreCase(b);
        }
        return false;
    }

    /**
     * 说明：创建文件夹
     * @param directory
     * @return  true创建成功
     */
    public static boolean mkdirs(File directory){
        try {
            forceMkdir(directory);
            return true;
        }catch (IOException e){
            Utils.eLog(e);
            return false;
        }
    }

    private static void forceMkdir(File directory) throws IOException {
        if (directory == null){
            throw new IOException("message = "+ " directory is null !");
        }
        if (directory.exists()){
            if (!directory.isDirectory()){
                throw new IOException("message = "+directory+" exists and is not BaseContract directory!");
            }
        }else {
            if (!directory.mkdirs()){
                if (!directory.isDirectory()){
                    throw new IOException("Unable to create directory "+directory);
                }
            }
        }
    }

    /**
     * 说明：删除指定文件
     * @param file
     * @return
     */
    public static boolean delete(File file){
        boolean flag = true;
        if (file != null && file.exists()){
            flag = file.delete();
        }
        return flag;
    }

    /**
     * 说明：关闭流
     *
     * @param closeables
     */
    public static void closeIO(Closeable... closeables) {
        if (null != closeables && closeables.length > 0) {
            for (Closeable closeable : closeables) {
                try {
                    if (closeable != null) {
                        closeable.close();
                    }
                } catch (Exception e) {
                    eLog(e);
                }
            }
        }
    }

    public static boolean checkFile(File file){
        if (file == null || !file.exists() || file.length() == 0 || !file.isFile()){
            return false;
        }
        return true;
    }

    public static void vLog(String message){
        if (HttpConfig.get().isDebug() && !TextUtils.isEmpty(message)){
            Log.v(HttpConfig.Constant.TAG, message);
        }
    }

    public static void eLog(Exception e){
        if (HttpConfig.get().isDebug() && e != null && !TextUtils.isEmpty(e.getMessage())){
            Log.e(HttpConfig.Constant.TAG, e.getMessage());
        }
    }
}
